﻿// -----------------------------------------------------------------------
// <copyright file="SaveFormats.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ImageP
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
 
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class SaveFormats
    {
        /// <summary>
        /// user's option on which extension wanted.
        /// </summary>
        private int index;
        /// <summary>
        /// the image to be saved.
        /// </summary>
        private Image picToSave;

        /// <summary>
        /// the image's name and location.
        /// </summary>
        private string fileName;

        /// <summary>
        /// type of image whether common format or PPM file.
        /// </summary>
        private string imgType;

        /// <summary>
        /// the image in the form of a bitmap.
        /// </summary>
        private Bitmap imgBitmap;

        /// <summary>
        /// the comment found in the PPM file.
        /// </summary>
        private string comment;

        /// <summary>
        /// flag indicating whether the PPM file has comment or not.
        /// </summary>
        private int flag;
       
        /// <summary>
        /// checks the format of the image whether common format or not.
        /// </summary>
        /// <param name="index">index</param>
        /// <param name="picToSave">picToSave</param>
        /// <param name="fileName">filename</param>
        /// <param name="imgType">imgType</param>
        /// <param name="comment">comment</param>
        /// <param name="flag">flag</param>
        public void CheckFormat(int index, Image picToSave, string fileName, string imgType, string comment, int flag)
        {
            this.flag = flag;
            this.comment = comment;
            this.imgType = imgType;
            this.picToSave = picToSave;
            this.fileName = fileName;
            if (this.index == 1 || this.index == 2 || this.index == 4 || this.index == 5)
            {
                this.SaveCommonFormat();
            }
            else if (this.index == 3)
            {
                this.SaveOtherFormat();
            }
        }

        /// <summary>
        /// saves images with common format.
        /// </summary>
        public void SaveCommonFormat()
        {
            Bitmap bm = new Bitmap(this.picToSave);
            bm.Save(this.fileName);
        }

        /// <summary>
        /// saves images of format PPM.
        /// </summary>
        public void SaveOtherFormat()
        {
            if (this.imgType == "p3")
            {
                this.WriteP3ToFile();
            }
            else if (this.imgType == "p6" || this.imgType == "P6")
            {
                this.WriteP6ToFile();
            }
        }

        /// <summary>
        /// writes the bitmap to file of type P3.
        /// </summary>
        public void WriteP3ToFile()
        {
            this.imgBitmap = new Bitmap(this.picToSave);
            int height = this.imgBitmap.Height;
            int width = this.imgBitmap.Width;
            StreamWriter stream = new StreamWriter(this.fileName);
            stream.WriteLine("P3");
            stream.WriteLine(this.comment);
            stream.Write(width);
            stream.Write(" ");
            stream.WriteLine(height);
            stream.WriteLine("255");

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    stream.Write(this.imgBitmap.GetPixel(j, i).R);
                    stream.Write(" ");
                    stream.Write(this.imgBitmap.GetPixel(j, i).R);
                    stream.Write(" ");
                    stream.Write(this.imgBitmap.GetPixel(j, i).R);
                    stream.Write(" ");
                }

                stream.WriteLine();
            }

            stream.Close();
        }

        /// <summary>
        /// writes the bitmap to file of type P3.
        /// </summary>
        public void WriteP6ToFile()
        {
            this.imgBitmap = new Bitmap(this.picToSave);
            int height = this.imgBitmap.Height;
            int width = this.imgBitmap.Width;
            FileStream fs = new FileStream(this.fileName, FileMode.Create, FileAccess.Write);
           
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    fs.WriteByte(Convert.ToByte(this.imgBitmap.GetPixel(j, i).R));
                    fs.WriteByte(Convert.ToByte(this.imgBitmap.GetPixel(j, i).G));
                    fs.WriteByte(Convert.ToByte(this.imgBitmap.GetPixel(j, i).B));
                }
            }

            fs.Close();
        }
    }
}
